package com.itheima.homework7;

import java.io.*;
import java.util.ArrayList;

public class Demo7_Maths_Expand {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(
                new FileReader("d13_zuoye_IO_String/maths.txt"));
        ArrayList<String> list = new ArrayList<>();
        //split1.0
        String str;
        while ((str = br.readLine()) != null) {
            if (str.contains("+")) {
                String[] split = str.split("\\+");
                int sum = Integer.parseInt(split[0]) + Integer.parseInt(split[1]);
                String strings = str + "=" + sum;
                list.add(strings);
            } else if (str.contains("-")) {
                String[] split = str.split("-");
                int cha = Integer.parseInt(split[0]) - Integer.parseInt(split[1]);
                String strings = str + "=" + cha;
                list.add(strings);
            } else if (str.contains("*")) {
                String[] split = str.split("\\*");
                int multifly = Integer.parseInt(split[0]) * Integer.parseInt(split[1]);
                String strings = str + "=" + multifly;
                list.add(strings);
            } else if (str.contains("/")) {
                String[] split = str.split("/");
                int devide = Integer.parseInt(split[0]) / Integer.parseInt(split[1]);
                String strings = str + "=" + devide;
                list.add(strings);
            }
        }
        br.close();
        BufferedWriter bw = new BufferedWriter(
                new FileWriter("d13_zuoye_IO_String/maths.txt"));
        for (String s : list) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();
        //substring2.0
        /*String str;
        while ((str = br.readLine()) != null) {
            if (str.contains("+")) {
                int index = str.indexOf("+");
                String s1 = str.substring(0, index);
                String s2 = str.substring(index + 1, str.length());
                int sum = Integer.parseInt(s1) + Integer.parseInt(s2);
                String strings = str + "=" + sum;
                list.add(strings);
            } else if (str.contains("-")) {
                int index = str.indexOf("-");
                String s1 = str.substring(0, index);
                String s2 = str.substring(index + 1, str.length());
                int cha = Integer.parseInt(s1) - Integer.parseInt(s2);
                String strings = str + "=" + cha;
                list.add(strings);
            } else if (str.contains("*")) {
                int index = str.indexOf("*");
                String s1 = str.substring(0, index);
                String s2 = str.substring(index + 1, str.length());
                int multifly = Integer.parseInt(s1) * Integer.parseInt(s2);
                String strings = str + "=" + multifly;
                list.add(strings);
            } else if (str.contains("/")) {
                int index = str.indexOf("/");
                String s1 = str.substring(0, index);
                String s2 = str.substring(index + 1, str.length());
                int devide = Integer.parseInt(s1) / Integer.parseInt(s2);
                String strings = str + "=" + devide;
                list.add(strings);
            }
        }
        br.close();
        BufferedWriter bw = new BufferedWriter(
                new FileWriter("d13_zuoye_IO_String/maths.txt"));
        for (String s : list) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();*/
        //substring1.0
        /*File file = new File("d13_zuoye_IO_String/maths.txt");
        file.delete();
        BufferedWriter bw = new BufferedWriter(
                new FileWriter("d13_zuoye_IO_String/maths.txt", true));
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            if (str.contains("+")) {
                int index = str.indexOf("+");
                String s1 = str.substring(0, index);
                String s2 = str.substring(index + 1, str.length());
                int sum = Integer.parseInt(s1) + Integer.parseInt(s2);
                String strings = s1 + "+" + s2 + "=" + sum;
                bw.write(strings);
                bw.newLine();
                bw.flush();
            } else if (str.contains("-")) {
                int index = str.indexOf("-");
                String s1 = str.substring(0, index);
                String s2 = str.substring(index + 1, str.length());
                int cha = Integer.parseInt(s1) - Integer.parseInt(s2);
                String strings = s1 + "-" + s2 + "=" + cha;
                bw.write(strings);
                bw.newLine();
                bw.flush();
            } else if (str.contains("*")) {
                int index = str.indexOf("*");
                String s1 = str.substring(0, index);
                String s2 = str.substring(index + 1, str.length());
                int multifly = Integer.parseInt(s1) * Integer.parseInt(s2);
                String strings = s1 + "*" + s2 + "=" + multifly;
                bw.write(strings);
                bw.newLine();
                bw.flush();
            } else if (str.contains("/")) {
                int index = str.indexOf("/");
                String s1 = str.substring(0, index);
                String s2 = str.substring(index + 1, str.length());
                int devide = Integer.parseInt(s1) / Integer.parseInt(s2);
                String strings = s1 + "/" + s2 + "=" + devide;
                bw.write(strings);
                bw.newLine();
                bw.flush();
            }
        }
        bw.close();*/
    }
}
